package com.angus.modules.content.controller.manage;

import com.angus.common.core.domain.R;
import com.angus.common.core.web.controller.BaseController;
import com.angus.common.core.web.domain.AjaxResult;
import com.angus.common.core.web.page.TableDataInfo;
import com.angus.common.log.annotation.Log;
import com.angus.common.log.enums.BusinessType;
import com.angus.modules.content.domain.ENContent;
import com.angus.modules.content.service.IENContentService;
import com.angus.modules.content.vo.ContentResultVo;
import com.angus.modules.content.vo.ContentResultVoV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author liyanan
 * @date 2024/10/22 1:28 PM
 * @description
 */
@RestController
@RequestMapping("/en")
public class ENContentController extends BaseController {

    @Autowired
    private IENContentService contentService;

    @Log(title = "内容管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,
                                 @RequestParam(name = "courseCode")
                                 @NotBlank(message = "课程编码不能为空")
                                 String courseCode) {

        String message = contentService.importContent(file,courseCode);
        return success(message);
    }

    @GetMapping("/getContentList")
    public TableDataInfo getContentCNList(@RequestParam @NotBlank(message = "课程编码不能为空") String courseCode,
                                          @RequestParam(required = false) Integer unitNum){
        startPage();
        List<ENContent> list = contentService.getContentList(courseCode,unitNum);
        return getDataTable(list);
    }

    @GetMapping("/getContentByGroupId")
    public R<ContentResultVo> getContentByGroupId(@RequestParam String courseCode,
                                                  @RequestParam Integer unitNum,
                                                  @RequestParam Long groupId){
        ContentResultVo resultVo = contentService.getContentByGroupId(courseCode, unitNum, groupId);
        return R.ok(resultVo);
    }

    @PostMapping("/addEN")
    public AjaxResult addEN(@RequestBody ENContent enContent){
        return contentService.addContent(enContent);
    }

    @PostMapping("/updateEN")
    public AjaxResult updateEN(@RequestBody ENContent enContent){
        return AjaxResult.success(contentService.updateContent(enContent));
    }

    @Log(title = "内容管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseCode}/{unitNum}/remove")
    public AjaxResult removeEN(@PathVariable("unitNum") Integer unitNum,
                               @PathVariable("courseCode") String courseCode,
                               @RequestBody List<Long> ids)
    {
        return AjaxResult.success(contentService.deleteByIds(ids,unitNum,courseCode));
    }

    @GetMapping("/getContentByGroupFlag")
    public R<ContentResultVoV2> getContentByGroupFlag(@RequestParam String courseCode,
                                            @RequestParam Integer unitNum,
                                            @RequestParam String groupFlag){
        ContentResultVoV2 resultVo = contentService.getContentByGroupFlag(courseCode, unitNum, groupFlag);
        return R.ok(resultVo);
    }

    @GetMapping("syncSortID")
    public AjaxResult syncSortID(){
        return contentService.syncSortID();
    }
}
